/*
 * JobFrame.java
 */



package gui;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.UIManager;

import model.Job;
import model.ListManager;
import model.Park;
import exceptions.UPException;

/**
 * JobFrame is a JFrame that displays 
 * 
 * @author Sean Brown
 * @version 1.0
 */
@SuppressWarnings("serial")
public class JobFrame extends JFrame {
	
	/**
	 * The ListManager for this instance of the program.
	 */
	private final ListManager my_lm;
	
	/**
	 * The ID of the Job to display
	 */
	private final int my_id;
	
	/**
	 * The Job for the ID passed.
	 */
	private Job my_job;
	
	/**
	 * The Park for the Job for the ID passed.
	 */
	private Park my_park;
	
	/**
	 * The JPanel that hold the Job info
	 */
	private JPanel my_job_panel;
	
	/**
	 * The JPanel that shows the Volunteers for the Job
	 */
	private JPanel my_vol_panel;
	
	/**
	 * Constructor instantiates fields and sets up the JFrame.
	 * 
	 * @param the_lm The ListManager to get data from.
	 * @param the_id The ID of the Job to display.
	 */
	public JobFrame(final ListManager the_lm, final int the_id) {
		super("Job Listing");
		my_id = the_id;
		my_lm = the_lm;
	}
	
	/**
	 * Sets up all panels, preferences, and shows the frame
	 * on screen.
	 */
	public void start() {
		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		UIManager.put("Panel.background", Color.WHITE);
		try {
			my_job = my_lm.findJob(my_id);
			my_park = my_lm.findPark(my_job.getParkId());
			UIManager.put("Panel.background", Color.WHITE);
			my_job_panel = jobPanel();
			my_vol_panel = volPanel();
			add(my_job_panel);
			pack();
			setLocationRelativeTo(getRootPane());
			setVisible(true);
		} catch (final UPException e) { //should not be reached
			GUIUtil.errorMessage(this, e.getMessage());
		}
	}

	/**
	 * Returns a JPanel with buttons for switching between the
	 * Job and Volunteer information panels.
	 * 
	 * @return JPanel to display
	 */
	private JPanel jobPanel() {
		//Buttons
		final JButton close = new JButton("Close");
		close.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e)
            {
                dispose();
            }
        });
		final JButton view_vols = new JButton("Volunteers");
		view_vols.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e)
            {
                remove(my_job_panel);
                add(my_vol_panel);
                pack();
                repaint();
                validate();
            }
        });
		final JPanel button_panel = new JPanel(new FlowLayout());
		button_panel.add(view_vols);
		button_panel.add(close);

		//set up JPanel
		JScrollPane scroller = new JScrollPane
				(jobDescPanel(), JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, 
						JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
		final JPanel entire_panel = new JPanel(new BorderLayout());
		entire_panel.add(scroller, BorderLayout.CENTER);
		entire_panel.add(button_panel, BorderLayout.SOUTH);
		return entire_panel;
	}
	
	/**
	 * Returns a JPanel with buttons for switching between the
	 * Volunteer and Job information panels.
	 * 
	 * @return JPanel to display
	 */
	private JPanel volPanel() {
		final JButton close = new JButton("Close");
		close.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e)
            {
                dispose();
            }
        });
		final JButton back = new JButton("Back");
		back.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e)
            {
                remove(my_vol_panel);
                add(my_job_panel);
                pack();
                repaint();
                validate();
            }
        });
		final JPanel button_panel = new JPanel(new FlowLayout());
		button_panel.add(back);
		button_panel.add(close);
		
		JScrollPane scroller = new JScrollPane
				(volNamePanel(), JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, 
						JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
		final JPanel entire_panel = new JPanel(new BorderLayout());
		entire_panel.add(scroller, BorderLayout.CENTER);
		entire_panel.add(button_panel, BorderLayout.SOUTH);
		return entire_panel;
	}

	/**
	 * Returns a JPanel with the data for a Job.
	 * 
	 * @return JPanel to display
	 */
	private JPanel jobDescPanel() {
		//Job description
		final JTextArea park_name = new JTextArea(my_park.getName());
		park_name.setEditable(false);
		park_name.setFont(GUIUtil.largerFont(park_name));
		final JTextArea address = 
				new JTextArea(my_park.getAddress().toString());
		address.setEditable(false);
		final JTextArea phone = new JTextArea("Phone: " + 
				my_park.getPhoneNumber().toString());
		phone.setEditable(false);
		final JTextArea dist_reg = new JTextArea();
		dist_reg.append("District: " + my_park.getDistrict());
		dist_reg.append(", Region: " + my_park.getRegion());
		dist_reg.setEditable(false);
		final JTextArea job_type = 
				new JTextArea("Job Type: " + my_job.getName());
		job_type.setEditable(false);
		job_type.setFont(new Font(job_type.getFont().getName(), 
				Font.BOLD, job_type.getFont().getSize()));
		final JTextArea start_end = new JTextArea();
		start_end.setEditable(false);
		start_end.append("Start\n" + 
				Job.dateToString(my_job.getStartTime()));
		start_end.append(", " + 
				Job.timeToString(my_job.getStartTime()));
		start_end.append("\nEnd\n" + 
				Job.dateToString(my_job.getEndTime()));
		start_end.append(", " + 
				Job.timeToString(my_job.getEndTime()));
		final JTextArea vol_label = new JTextArea("Volunteers:");
		vol_label.setEditable(false);
		vol_label.setFont(new Font(vol_label.getFont().getName(), 
				Font.BOLD, vol_label.getFont().getSize()));
		final JTextArea vols = new JTextArea(my_job.getSlotsToString());
		vols.setEditable(false);
		final JPanel job_panel = new JPanel();
		job_panel.setLayout(new BoxLayout(job_panel, BoxLayout.PAGE_AXIS));
		job_panel.add(park_name);
		job_panel.add(address);
		job_panel.add(phone);
		job_panel.add(dist_reg);
		job_panel.add(job_type);
		job_panel.add(start_end);
		job_panel.add(vol_label);
		job_panel.add(vols);
		return job_panel;
	}
	
	/**
	 * Returns a JPanel that contains the Volunteers
	 * portion for the Job and their workload with emails
	 * for contact.
	 * 
	 * @return JPanel to display.
	 */
	private JPanel volNamePanel() {
		final JTextArea title = new JTextArea("Volunteers");
		title.setFont(GUIUtil.largerFont(title));
		title.setEditable(false);
		final JTextArea heavy = new JTextArea("Heavy");
		heavy.setFont(new Font(heavy.getFont().getName(), 
				Font.BOLD, heavy.getFont().getSize()));
		heavy.setEditable(false);
		final JTextArea medium = new JTextArea("Medium");
		medium.setFont(new Font(medium.getFont().getName(), 
				Font.BOLD, medium.getFont().getSize()));
		medium.setEditable(false);
		final JTextArea light = new JTextArea("Heavy");
		light.setFont(new Font(light.getFont().getName(), 
				Font.BOLD, light.getFont().getSize()));
		light.setEditable(false);
		final JTextArea heavy_names = new JTextArea();
		final JTextArea med_names = new JTextArea();
		final JTextArea light_names = new JTextArea();
		try {
			for (int i : my_job.getHeavyVolunteerIDs()) {
				final String name = my_lm.findUser(i).getName();
				final String email = my_lm.findUser(i).getEmail();
				heavy_names.append(name + " - " + email + "\n");
			}
			heavy_names.setEditable(false);
			for (int i : my_job.getMediumVolunteerIDs()) {
				final String name = my_lm.findUser(i).getName();
				final String email = my_lm.findUser(i).getEmail();
				med_names.append(name + " - " + email + "\n");
			}
			med_names.setEditable(false);
			for (int i : my_job.getLightVolunteerIDs()) {
				final String name = my_lm.findUser(i).getName();
				final String email = my_lm.findUser(i).getEmail();
				light_names.append(name + " - " + email + "\n");
			}
			light_names.setEditable(false);
		} catch (final UPException upe) { //should not be reached
			GUIUtil.errorMessage(this, upe.getMessage());
		}
		final JPanel vol_panel = new JPanel();
		vol_panel.setLayout(new BoxLayout(vol_panel, BoxLayout.PAGE_AXIS));
		vol_panel.add(title);
		vol_panel.add(heavy);
		vol_panel.add(heavy_names);
		vol_panel.add(medium);
		vol_panel.add(med_names);
		vol_panel.add(light);
		vol_panel.add(light_names);
		return vol_panel;
	}
}
